
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><LINK rel="stylesheet" href="manpage.css"><TITLE>units</TITLE><META name="xsl-processor" content="SAXON 5.5 from Michael Kay of ICL"><META name="generator" content="$Id: units.html,v 1.1 2002/10/24 12:32:38 zahn Exp $"></HEAD><BODY><DIV class="header"><DIV class="navbar" align="center"><A HREF="#SECTb2ab3">NAME</A> &middot; <A HREF="#SECTb2ab5">SYNOPSIS</A> &middot; <A HREF="#SECTb2ab7">DESCRIPTION</A> &middot; <A HREF="#SECTb2ab9">COMMANDS</A> &middot; <A HREF="#SECTb2ac11">Unit String Format</A> &middot; <A HREF="#SECTb2ac13">SI UNITS</A> &middot; <A HREF="#SECTb2ac15">REFERENCES</A> &middot; <A HREF="#SECTb2ac17">KEYWORDS</A></DIV><HR class="navsep"></DIV><DIV class="body">


<h2><a name="SECTb2ab3">NAME</a></h2><p class="namesection"><B class="names">units - </B><BR>Convert and manipulate quantities with units</p>

<h2><a name="SECTb2ab5">SYNOPSIS</a></h2><PRE class="syntax">
package require units ?1.0?
units::convert <i class="m">value</i> <i class="m">targetUnits</i>
units::reduce <i class="m">unitString</i>
units::new <i class="m">name</i> <i class="m">baseUnits</i>
</PRE>


<h2><a name="SECTb2ab7">DESCRIPTION</a></h2><p>
This library provides a conversion facility from a variety of
scientific and engineering shorthand notations into floating point
numbers.  This allows application developers to easily convert
values with different units into uniformly scaled numbers.
</p><p>
The units conversion facility is also able to convert between
compatible units.  If, for example, a application is expecting a value
in <tt class="samp">ohms</tt> (Resistance), and the user specifies units of
<tt class="samp">milliwebers/femtocoulomb</tt>, the conversion routine will
handle it appropriately.  An error will be generated if an incorrect
conversion is attempted.
</p><p>
Values are scaled from one set of units to another by dimensional
analysis.  Both the value units and the target units are reduced into
primitive units and a scale factor.  Units are checked for
compatibility, and the scale factors are applied by multiplication and
division.  This technique is extremely flexible and quite robust.
</p><p>
New units and new unit abbreviations can be defined in terms of
existing units and abbreviations.  It is also possible to define a new
primitive unit, although that will probably be unnecessary.  New units
will most commonly be defined to accommodate non-SI measurement
systems, such as defining the unit <tt class="samp">inch</tt> as <tt class="samp">"2.54
cm"</tt>.
</p>


<h2><a name="SECTb2ab9">COMMANDS</a></h2><dl class="commandlist">


<dt>units::convert <i class="m">value</i> <i class="m">targetUnits</i> </dt>
<dd>
Converts the value string into a floating point number, scaled to the
specified <i class="m">targetUnits</i>.  The <i class="m">value</i> string may contain a
number and units.  If units are specified, then they must be
compatible with the <i class="m">targetUnits</i>.  If units are not specified
for the <i class="m">value</i>, then it will be scaled to the target units.  For
example, scaling <tt class="samp">"1.0"</tt> to <tt class="samp">"millimeter"</tt> would
result in <tt class="samp">"1000.0"</tt>.
</dd>




<dt>units::reduce <i class="m">unitString</i> </dt>
<dd>
Returns a unit string consisting of a scale factor followed by a space
separated list of sorted and reduced primitive units.  The reduced
unit string may include a forward-slash (separated from the
surrounding primitive subunits by spaces) indicating that the
remaining subunits are in the denominator.  Generates an error if the
<i class="m">unitString</i> is invalid.
</dd>



<dt>units::new <i class="m">name</i> <i class="m">baseUnits</i> </dt>
<dd>
Creates a new unit conversion with the specified name.  The new unit
<i class="m">name</i> must be only alphabetic (upper or lower case) letters.
The <i class="m">baseUnits</i> string can consist of any valid units conversion
string, including constant factors, numerator and denominator parts,
units with prefixes, and exponents.  The baseUnits may contain any
number of subunits, but it must reduce to primitive units.  BaseUnits
could also be the string <tt class="samp">"-primitive"</tt> to represent a new
kind of quantity which cannot be derived from other units.  But you
probably would not do that unless you have discovered some kind of new
univeral property.
</dd>


</dl>


<h2><a name="SECTb2ac11">Unit String Format</a></h2><p>
Value and unit string format is quite flexible.  It is possible to
define virtually any combination of units, prefixes, and powers.
Valid unit strings must conform to these rules.
</p><ul>
<li>
A unit string consists of an optional scale factor followed by zero or
more subunits.  The scale factor must be a valid floating point
number, and may or may not be separated from the subunits.  The scale
factor could be negative.
</li>
<li>
Subunits are separated form each other by one or more separator
characters, which are space (" "), hyphen ("-"), asterisk ("*"), and
forward-slash ("/").  Sure, go ahead and complain about using a minus
sign ("-") to represent multiplication.  It just isn't sound
mathematics, and, by rights, we should require everyone to use the
asterisk ("*") to separate all units.  But the bottom line is that
complex unit strings like <tt class="samp">"m-kg/s^2"</tt> are pleasantly
readable.
</li>
<li>
The forward-slash seperator ("/") indicates that following subunits are
in the denominator.  There can be at most one forward-slash separator.
</li>
<li>
Subunits can be floating point scale factors, but with the exception
of the leading scale factor, they must be surrounded by valid
separators.  Subunit scale factors cannot be negative. (Remember that
the hyphen is a unit separator.)
</li>
<li>
Subunits can be valid units or abbreviations.  They may include a
prefix.  They may include a plural suffix "s" or "es".  They may also
include a power string denoted by a circumflex ("^"), followed by a
integer, after the unit name (or plural suffix, if there is one).
Negative exponents are not allowed.  (Remember that the hyphen is a
unit separator.)
</li>
</ul> 
<h3><a name="SECTb2ac11b3"> Example valid unit strings </a></h3>
<PRE class="example">
Unit String              Reduced Unit String
------------------------------------------------------------
meter                    1.0 meter
kilometer		 1000.0 meter
km			 1000.0 meter
km/s                     1000.0 meter / second
/microsecond             1000000.0 / second
/us			 1000000.0 / second
kg-m/s^2		 1000.0 gram meter / second second
30second		 30.0 second
30 second		 30.0 second
30 seconds		 30.0 second
200*meter/20.5*second	 9.75609756098 meter / second
</PRE>





<h2><a name="SECTb2ac13">SI UNITS</a></h2><p>
The standard SI units are predefined according to <em>NIST Special
Publication 330</em>.  Standard units for both SI Base Units (Table
1) and SI Derived Units with Special Names (Tables 3a and 3b) are
included here for reference.  Each standard unit name and abbreviation
are included in this package.
</p>
<h3><a name="SECTb2ac13b2">SI Base Units</a></h3>
<PRE class="example">
Quantity		Unit Name    Abbr.
---------------------------------------------
Length			meter	     m
Mass			kilogram     kg
Time			second	     s
Current			ampere	     A
Temperature		kelvin	     K
Amount			mole	     mol
Luminous Intensity	candela	     cd
</PRE>

<h3><a name="SECTb2ac13b3">SI Derived Units with Special Names</a></h3>
<PRE class="example">
Quantity		Unit Name    Abbr.   Units     Base Units
--------------------------------------------------------------------
plane angle             radian      rad     m/m       m/m
solid angle		steradian   sr      m^2/m^2   m^2/m^2
frequency		hertz	    Hz	              /s
force			newton	    N                 m-kg/s^2
pressure		pascal	    Pa      N/m^2     kg/m-s^2
energy, work		joule	    J       N-m       m^2-kg/s^2
power, radiant flux	watt	    W       J/s       m^2-kg/s^3
electric charge         coulomb	    C                 s-A
electric potential	volt	    V       W/A       m^2-kg/s^3-A
capacitance		farad	    F       C/V       s^4-A^2/m^2-kg
electric resistance	ohm                 V/A       m^2-kg/s^3-A^2
electric conductance	siemens	    S       A/V       s^3-A^2/m^2-kg
magnetic flux		weber	    Wb      V-s       m^2-kg/s^2-A
magnetic flux density	tesla	    T       Wb/m^2    kg/s^2-A
inductance		henry	    H       Wb/A      m^2-kg/s^2-A^2
luminous flux		lumen	    lm                cd-sr
illuminance		lux	    lx      lm/m^2    cd-sr/m^2
activity (of a
radionuclide)           bacquerel   Bq                /s
absorbed dose           gray        Gy      J/kg      m^2/s^2
dose equivalent         sievert     Sv      J/kg      m^2/s^2
</PRE>

<p>
Note that the SI unit kilograms is actually implemented as grams
because 1e-6 kilogram = 1 milligram, not 1 microkilogram.  The
abbreviation for Electric Resistance (ohms), which is the omega
character, is not supported.  Also note that there is no support for
Celsius temperature.  The units conversion routines can only scale
values with multiplication and division, and the abbreviation "C"
(which is the closest one could get to "degrees C" using the degrees
symbol) is already used by Electric Potential.
</p>
<p>
SI Units can have a multiple or sub-multiple prefix.  The prefix or its
abbreviation should appear before the unit, without spaces.  Compound
prefixes are not allowed, and a prefix should never be used alone.
These prefixes are defined in Table 5 of <em>Special Publication
330</em>.
</p>
<h3><a name="SECTb2ac13b5">SI Prefixes</a></h3>
<PRE class="example">
Prefix Name	Abbr.   Factor
---------------------------------------
yotta		Y	1e24
zetta		Z	1e21
exa		E	1e18
peta		P	1e15
tera		T	1e12
giga		G	1e9
mega		M	1e6
kilo		k	1e3
hecto		h	1e2
deka		da	1e1
deca			1e1

deci		d	1e-1
centi		c	1e-2
milli		m	1e-3
micro		u	1e-6
nano		n	1e-9
pico		p	1e-12
femto		f	1e-15
atto		a	1e-18
zepto		z	1e-21
yocto		y	1e-24
</PRE>
<p>
Note that we define the same prefix with both the USA ("deka") and
non-USA ("deca") spellings.  Also note that we take the liberty of
allowing "micro" to be typed as a "u" instead of the Greek character
mu.
</p><p>
Many non-SI units are commonly used in applications.  Appendix B.8 of
<em>NIST Special Publication 811</em> lists many non-SI conversion
factors.  It is not possible to include all possible unit definitions
in this package.  In some cases, many different conversion factors
exist for a given unit, depending on the context. (The appendix lists
over 40 conversions for British thermal units!)  Application specific
conversions can always be added using the <b class="command">new</b>
command, but some well known and often used conversions are included
in this package.
</p>
<h3><a name="SECTb2ac13b8">Non-SI Units</a></h3>
<PRE class="example">
Unit Name	     Abbr.    Base Units
--------------------------------------------------
angstrom                      1.0E-10 m
astronomicalUnit     AU       1.495979E11 m
atmosphere                    1.01325E5 Pa
bar                           1.0E5 Pa
calorie                       4.1868 J
curie                         3.7E10 Bq
day                           8.64E4 s
degree                        1.745329E-2 rad
erg                           1.0E-7 J
faraday                       9.648531 C
fermi                         1.0E-15 m
foot                 ft       3.048E-1 m
gauss                         1.0E-4 T
gilbert                       7.957747E-1 A
grain                gr       6.479891E-5 kg
hectare              ha       1.0E4 m^2
hour                 h        3.6E3 s
inch                 in       2.54E-2 m
lightYear                     9.46073E15 m
liter                L        1.0E-3 m^3
maxwell              Mx       1.0E-8 Wb
mho                           1.0 S
micron                        1.0E-6 m
mil                           2.54E-5 m
mile                 mi       1.609344E3 m
minute		     min      6.0E1 s
parsec               pc       3.085E16 m
pica                          4.233333E-3 m
pound                lb       4.535924E-1 kg
revolution                    6.283185 rad
revolutionPerMinute  rpm      1.047198E-1 rad/s
yard                          9.144E-1 m
year                 yd       3.1536E7 s
</PRE>

<h3><a name="SECTb2ac13b9">Quantities and Derived Units with Special Names</a></h3>

<p>
This units conversion package is limited specifically to unit
reduction, comparison, and scaling.  This package does not consider
any of the quantity names for either base or derived units.  A similar
implementation or an extension in a typed or object-oriented language
might introduce user defined types for the quantities.  Quantity type
checking could be used, for example, to ensure that all
<tt class="samp">"length"</tt> values properly reduced to <tt class="samp">"meters"</tt>, or
that all <tt class="samp">"velocity"</tt> values properly reduced to
<tt class="samp">"meters/second"</tt>.
</p>

<p>
A C implementation of this package has been created to work in
conjunction with the Simplified Wrapper Interface Generator
(http://www.swig.org).  That package (units.i) exploits SWIG's typemap
system to automatically convert script quantity strings into floating
point quantities.  Function arguments are specified as quantity types
(e.g., <tt class="samp">typedef float Length</tt>), and target units (expected
by the C application code) are specified in an associative array.
Default units are also defined for each quantity type, and are applied
to any unit-less quantity strings.
</p>

<p>
A units system enhanced with quantity type checking might benefit from
inclusion of other derived types which are expressed in terms of
special units, as illustrated in Table 2 of <em>NIST Publication
330</em>.  The quantity <tt class="samp">"area"</tt>, for example, could be
defined as units properly reducing to <tt class="samp">"meter^2"</tt>, although
the utility of defining a unit named <tt class="samp">"square meter"</tt> is
arguable.
</p>


<h2><a name="SECTb2ac15">REFERENCES</a></h2><p>
The unit names, abbreviations, and conversion values are derived from
those published by the United States Department of Commerce Technology
Administration, National Institute of Standards and Technology (NIST)
in <em>NIST Special Publication 330: The International System of
Units (SI)</em> and <em>NIST Special Publication 811: Guide for
the Use of the International System of Units (SI)</em>.  Both of
these publications are available (as of December 2000) from
http://physics.nist.gov/cuu/Reference/contents.html
</p><p>
The ideas behind implementation of this package is based in part on
code written in 1993 by Adrian Mariano which performed dimensional
analysis of unit strings using fixed size tables of C structs.
</p>

<h2><a name="SECTb2ac17">KEYWORDS</a></h2><P class="keywords"><A class="keyword" href="keyword-index.html#KW-unit">unit</A>, <A class="keyword" href="keyword-index.html#KW-convert">convert</A></P>

</DIV><DIV class="footer"><HR class="navsep"><!-- footer.html: Standard navigational footer --><div class="navbar" align="center">
	<a class="navaid" href="index.html">Table of Contents</a>
 &middot;	<a class="navaid" href="command-index.html">Alphabetical Index</a>
 &middot;	<a class="navaid" href="keyword-index.html">Keyword Index</a>
 &middot;	<a class="navaid" href="category-index.html">Category Index</a>
 &middot;	<a class="navaid" href="../About.html">About...</a>
</div></DIV></BODY></HTML>